#include "RouletteWheelSelection.hh"

RouletteWheelSelection::RouletteWheelSelection() {}

RouletteWheelSelection::~RouletteWheelSelection() {}

Solution* RouletteWheelSelection::select(Population *population) {
    double random = Random::getInstance()->getReal(0, population->getSum());
    Solution* selected = NULL;
    double sumTemp = 0;

    for (int i = 0; i < population->length() - 1; i++) {
        Solution *solution = population->get(i);
        //cout << solution->getFitness() << " ";
        if (sumTemp + solution->getFitness() > random) {

            selected = solution;
            break;

        } else sumTemp = sumTemp + solution->getFitness();
    }


    if (selected == NULL) selected = population->get(population->length() - 1);

    return selected->copy();
}
